package com.shopmall.controller;

import com.shopmall.pojo.Admin;
import com.shopmall.pojo.Goods;
import com.shopmall.pojo.GoodsInList;
import com.shopmall.service.GoodsInListServiceImpl;
import com.shopmall.service.GoodsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.List;

@Controller
public class GoodsInController {

    @Autowired
    GoodsServiceImpl goodsService;

    @Autowired
    GoodsInListServiceImpl goodsInListService;

    @RequestMapping("/goods/goods_in")
    public String goods_in(Model model){
        List<Goods> goodsList=goodsService.queryGoodsList();
        model.addAttribute("goodsList",goodsList);
        return "goods/goods_in";
    }


    @RequestMapping("/goods/goods_in_do")
    public String goods_in_do(GoodsInList goodsInList,HttpSession session,Model model){
        Admin admin=(Admin)session.getAttribute("admin");
        String do_who=admin.getUsername();
        goodsInList.setDo_who(do_who);
        Timestamp do_time = new Timestamp(System.currentTimeMillis());
        goodsInList.setDo_time(do_time);

        Goods goods = new Goods(); //添加进货单的同时，要修改到商品实体那边的数量以及日期
        goods.setGoods_id(goodsInList.getGoods_id());
        goods.setNum(goodsInList.getNum());
        goods.setUpdate_date(do_time);

        goodsInListService.addGoodsInList(goodsInList);
        goodsService.goodsIn(goods);

        model.addAttribute("msg",
                "成功给编号为 "+goodsInList.getGoods_id()+" 的商品进货！");

        return "redirect:/goods/goods_show";
    }

    @RequestMapping("/goods/goods_in_mem")
    public String goods_in_mem(Model model){
        List<GoodsInList> goods_in_list = goodsInListService.queryGoodsInListList();
        model.addAttribute("goods_in_list", goods_in_list);
        return "goods/goods_in_mem";
    }

    @RequestMapping("/goods/delete_goods_in_list/{id}")
    public String delete_goods_in_list(@PathVariable("id") int id, Model model){
        goodsInListService.deleteGoodsInList(id);
        model.addAttribute("成功删除编号为 "+id+" 的进货记录！");
        return "redirect:/goods/goods_in_mem";
    }

}
